package com.oim.fx.view;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.oim.core.app.AppContext;
import com.oim.core.bean.UserData;
import com.oim.core.business.sender.PersonalSender;
import com.oim.core.business.sender.UserSender;
import com.oim.core.common.app.view.AbstractView;
import com.oim.core.common.app.view.MainView;
import com.oim.core.common.app.view.UserDataEditView;
import com.oim.core.common.box.PersonalBox;
import com.oim.core.net.server.Back;
import com.oim.fx.common.box.ImageBox;
import com.oim.fx.ui.HeadChooseFrame;
import com.oim.fx.ui.UserEditFrame;
import com.oim.fx.ui.component.WaitingPanel;
import com.oim.fx.ui.list.HeadImagePanel;
import com.oim.fx.ui.user.HeadListPanel;
import com.only.common.result.Info;
import com.only.net.data.action.DataBackActionAdapter;

import javafx.application.Platform;
import javafx.scene.image.Image;

/**
 * 描述：
 * 
 * @author XiaHui
 * @date 2015年3月16日 下午10:42:19
 * @version 0.0.1
 */
public class UserDataEditViewImpl extends AbstractView implements UserDataEditView {

	UserEditFrame frame;
	HeadChooseFrame hcf;
	String head;
	public UserDataEditViewImpl(AppContext appContext) {
		super(appContext);
		initUI();
	}

	private void initUI() {
		Platform.runLater(new Runnable() {
			@Override
			public void run() {
				frame = new UserEditFrame();
				frame.setTitleText("个人资料修改");
				hcf=new HeadChooseFrame();
				initEvent();
			}
		});
	}

	protected void initEvent() {
		frame.setDoneAction(a -> {
			update();
		});
		frame.setOnHeadMouseClicked(m->{
			hcf.show();
			hcf.toFront();
		});
		HeadListPanel p	=hcf.getHeadListPanel();
		p.showWaiting(true, WaitingPanel.show_waiting);
		List<HeadImagePanel> list = new ArrayList<HeadImagePanel>();
		for (int i = 1; i < 102; i++) {
			Image image = ImageBox.getImagePath("Resources/Images/Head/User/" + i + "_100.gif");
			String value = i + "";
			HeadImagePanel hp = new HeadImagePanel();
			hp.setHeadSize(60);
			hp.setImage(image);
			hp.setOnMouseClicked(m -> {
				select(image, value);
			});
			list.add(hp);

		}

		for (int i = 173; i < 265; i++) {
			Image image = ImageBox.getImagePath("Resources/Images/Head/User/" + i + "_100.gif");
			String value = i + "";
			HeadImagePanel hp = new HeadImagePanel();
			hp.setHeadSize(60);
			hp.setImage(image);
			hp.setOnMouseClicked(m -> {
				select(image, value);
			});
			list.add(hp);

		}
		p.showWaiting(false, WaitingPanel.show_waiting);
		p.setNodeList(list);
	}

	private void select(Image image, String value) {
		hcf.select(b->{
			hcf.hide();
			frame.setHeadImage(image);
			head=value;
		});
	}

	@Override
	public void setVisible(boolean visible) {
		Platform.runLater(new Runnable() {
			@Override
			public void run() {
				if (visible) {
					frame.show();
					frame.toFront();
					setUserData(frame);
				} else {
					frame.hide();
				}
			}
		});
	}

	private void setUserData(UserEditFrame frame) {
		UserData user = PersonalBox.get(UserData.class);
		head=user.getHead();
		Image image = ImageBox.getImagePath("Resources/Images/Head/User/" +head + "_100.gif");
		
		
		String nickname = user.getNickname();
		String gender = user.getGender();
		String blood = user.getBlood();
		Date birthdate = user.getBirthdate();
		String homeAddress = user.getHomeAddress();
		String locationAddress = user.getLocationAddress();
		String mobile = user.getMobile();
		String email = user.getEmail();
		String signature = user.getSignature();
		String introduce = user.getIntroduce();
		
		frame.setHeadImage(image);
		frame.setNickname(nickname);
		frame.setGender(gender);
		frame.setBloodType(blood);
		frame.setBirthday(birthdate);
		frame.setHomeAddress(homeAddress);
		frame.setLocationAddress(locationAddress);
		frame.setMobile(mobile);
		frame.setEmail(email);
		frame.setSignature(signature);
		frame.setIntroduce(introduce);

	}

	public void update() {
		boolean v = frame.verify();
		if (!v) {
			return;
		}
		String nickname = frame.getNickname();
		String gender = frame.getGender();
		String blood = frame.getBloodType();
		Date birthdate = frame.getBirthday();
		String homeAddress = frame.getHomeAddress();
		String locationAddress = frame.getLocationAddress();
		String mobile = frame.getMobile();
		String email = frame.getEmail();
		String signature = frame.getSignature();
		String introduce = frame.getIntroduce();

		if (null == nickname || "".equals(nickname)) {
			showPromptMessage("昵称不能空！");
			return;
		}
		UserData userData = PersonalBox.get(UserData.class);
		
		UserData user = new UserData();
		user.setId(userData.getId());
		user.setHead(head);
		user.setNickname(nickname);
		user.setGender(gender);
		user.setBlood(blood);
		user.setBirthdate(birthdate);
		user.setHomeAddress(homeAddress);
		user.setLocationAddress(locationAddress);
		user.setMobile(mobile);
		user.setEmail(email);
		user.setSignature(signature);
		user.setIntroduce(introduce);

		DataBackActionAdapter action = new DataBackActionAdapter() {

			@Back
			public void back(Info info) {
				if (info.isSuccess()) {
					userData.setHead(head);
					userData.setNickname(nickname);
					userData.setGender(gender);
					userData.setBlood(blood);
					userData.setBirthdate(birthdate);
					userData.setHomeAddress(homeAddress);
					userData.setLocationAddress(locationAddress);
					userData.setMobile(mobile);
					userData.setEmail(email);
					userData.setSignature(signature);
					userData.setIntroduce(introduce);
					setVisible(false);
					MainView mainView = appContext.getSingleView(MainView.class);
					//mainView.setVisible(true);
					mainView.setUser(userData);
					UserSender us=appContext.getSender(UserSender.class);
					us.sendUpdate();
				} else {
					setVisible(true);
					showPrompt("修改失败！");
				}
			}
		};
		PersonalSender ps = this.appContext.getSender(PersonalSender.class);
		ps.updateUser(user, action);
	}

	public void showPromptMessage(String text) {
		frame.showPrompt(text);
	}

	@Override
	public boolean isShowing() {
		return false;
	}
}
